package com.liuchuanzheng.lcz_animationdemos.tradition;

import android.content.Context;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;

import com.liuchuanzheng.lcz_animationdemos.R;
import com.liuchuanzheng.lcz_animationdemos.databinding.ActivityTweenAnimationBinding;
import com.liuchuanzheng.lcz_animationdemos.utils.LogUtil;

public class TweenAnimationActivity extends AppCompatActivity {
    private ActivityTweenAnimationBinding mViewBinding;
    private float pxValue, pyValue, deValue;
    private int timeValue;
    private Context mContext;
    private Animation animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewBinding = ActivityTweenAnimationBinding.inflate(getLayoutInflater());
        setContentView(mViewBinding.getRoot());
        mContext = this;
        mViewBinding.degree.setProgress(25);
        deValue = 360 * 25 / 100.0f;
        mViewBinding.time.setProgress(10);
        timeValue = 100 * 10;
        mViewBinding.dvalue.setText(String.valueOf(deValue));
        mViewBinding.tValue.setText(String.valueOf(timeValue) + "ms");
        mViewBinding.pivotX.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                pxValue = progress / 100.0f;
                mViewBinding.xvalue.setText(String.valueOf(pxValue));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mViewBinding.pivotY.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                pyValue = progress / 100.0f;
                mViewBinding.yvalue.setText(String.valueOf(pyValue));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mViewBinding.degree.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                LogUtil.i(progress + "");
                deValue = 360 * progress / 100.0f;
                mViewBinding.dvalue.setText(String.valueOf(deValue));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mViewBinding.time.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress <= 10) {
                    progress = 10;
                } else if (progress >= 100) {
                    progress = 100;
                }


                timeValue = 100 * progress;
                mViewBinding.tValue.setText(String.valueOf(timeValue) + " ms");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        mViewBinding.alpha.setOnClickListener(v -> {
            AlpahAnimation();
        });
        mViewBinding.translate.setOnClickListener(v -> {
            TranslationAnimation();
        });
        mViewBinding.rotate.setOnClickListener(v -> {
            RotateAnimation();
        });
        mViewBinding.scale.setOnClickListener(v -> {
            ScaleAnimation();
        });
        mViewBinding.stopAnim.setOnClickListener(v -> {
            mViewBinding.stopAnim.clearAnimation();
            if (animation != null) {
                animation.cancel();
                animation.reset();
            }
            mViewBinding.loop.setChecked(false);
            mViewBinding.reverse.setChecked(false);
            mViewBinding.keep.setChecked(false);

            mViewBinding.pivotX.setProgress(0);
            mViewBinding.pivotY.setProgress(0);
            mViewBinding.degree.setProgress(25);
            mViewBinding.time.setProgress(10);
        });

    }

    /**
     * alpha Animation
     */
    private void AlpahAnimation() {
        animation = AnimationUtils.loadAnimation(mContext, R.anim.alpha_anim);
        if (mViewBinding.keep.isChecked()) {
            animation.setFillAfter(true);
        } else {
            animation.setFillAfter(false);
        }
        if (mViewBinding.loop.isChecked()) {
            animation.setRepeatCount(-1);
        } else {
            animation.setRepeatCount(0);
        }

        if (mViewBinding.reverse.isChecked()) {
            animation.setRepeatMode(Animation.REVERSE);
        } else {
            animation.setRepeatMode(Animation.RESTART);
        }
        mViewBinding.img1.startAnimation(animation);
    }

    private void TranslationAnimation() {
        animation = AnimationUtils.loadAnimation(mContext, R.anim.translate_anim);
        animation.setInterpolator(new LinearInterpolator());

        if (mViewBinding.keep.isChecked()) {
            animation.setFillAfter(true);
        } else {
            animation.setFillAfter(false);
        }
        if (mViewBinding.loop.isChecked()) {
            animation.setRepeatCount(-1);
        } else {
            animation.setRepeatCount(0);
        }

        if (mViewBinding.reverse.isChecked()) {
            animation.setRepeatMode(Animation.REVERSE);
        } else {
            animation.setRepeatMode(Animation.RESTART);
        }
        mViewBinding.img1.startAnimation(animation);
    }

    /**
     * rotate Animation
     */
    private void RotateAnimation() {
        animation = new RotateAnimation(-deValue, deValue, Animation.RELATIVE_TO_SELF,
                pxValue, Animation.RELATIVE_TO_SELF, pyValue);
        animation.setDuration(timeValue);

        if (mViewBinding.keep.isChecked()) {
            animation.setFillAfter(true);
        } else {
            animation.setFillAfter(false);
        }
        if (mViewBinding.loop.isChecked()) {
            animation.setRepeatCount(-1);
        } else {
            animation.setRepeatCount(0);
        }

        if (mViewBinding.reverse.isChecked()) {
            animation.setRepeatMode(Animation.REVERSE);
        } else {
            animation.setRepeatMode(Animation.RESTART);
        }
        mViewBinding.img1.startAnimation(animation);
    }


    /**
     * scale Animation
     */
    private void ScaleAnimation() {
//        animation = null;
        if (mViewBinding.rb1.isChecked()) {
            animation = AnimationUtils.loadAnimation(mContext, R.anim.scale_anim1);
        } else if (mViewBinding.rb2.isChecked()) {
            animation = AnimationUtils.loadAnimation(mContext, R.anim.scale_anim2);
        } else if (mViewBinding.rb3.isChecked()) {
            animation = AnimationUtils.loadAnimation(mContext, R.anim.scale_anim3);
        }

        if (mViewBinding.keep.isChecked()) {
            animation.setFillAfter(true);
        } else {
            animation.setFillAfter(false);
        }
        if (mViewBinding.loop.isChecked()) {
            animation.setRepeatCount(-1);
        } else {
            animation.setRepeatCount(0);
        }


        mViewBinding.img1.startAnimation(animation);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.zoomout);
    }
}